//
//  StoreRoomTwoVC.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/9/12.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "StoreRoomTwoVC.h"

#import "StoreRoomTwoCell.h"
#import "TodoHttpManager.h"
#import "ZhuShouThreeVC.h"
@interface StoreRoomTwoVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tabView;
@property(nonatomic,strong)NSMutableArray *roomArr;



@end

@implementation StoreRoomTwoVC


-(void)viewDidAppear:(BOOL)animated
{
    //储藏室房源
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDCeoCeStoreRoom:) name:TDCeoCeStoreRoom object:nil];
    // TODO:储藏室房源房源
    [_manager SetUpCeoShenPiStoreRoomWithFloorId:_chuID];
    
    //优惠政策执行
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRoomAgreeAndUnAgree:) name:TDCeoCeAgree object:nil];
    //优惠政策驳回
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRoomAgreeAndUnAgree:) name:TDCeoCeUNAgree object:nil];
    
}
// 储藏室房源
-(void)didTDCeoCeStoreRoom:(NSNotification *)notifi
{
    if (notifi.object != 0)
    {
        _roomArr = [notifi.object objectForKey:@"data"];
        NSLog(@"------主售信息   %@----",_roomArr);
        [_tabView reloadData];
    }
}

//优惠政策的执行OR驳回
-(void)didRoomAgreeAndUnAgree:(NSNotification *)not
{
    if (not.object !=0 )
    {
        NSString *status = [not.object objectForKey:@"status"];
        NSString *info = [not.object objectForKey:@"info"];
        if ([status isEqualToString:@"1"])
        {
            [self.navigationController popViewControllerAnimated:NO];
        }
        else
        {
            [_manager MakeMyselfAlert:info andIsNeedTitle:NO andShowTime:2];
        }
    }
}
-(void)viewDidDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCeoCeStoreRoom object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCeoCeAgree object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCeoCeUNAgree object:nil];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"优惠政策审批";
    self.view.backgroundColor = BgColor;
    _manager = [TodoHttpManager sharedManager];
    _roomArr = [NSMutableArray array];
    
    [self setUIStoreRoomTwo];
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 105, B_WIDTH, B_HEIGHT-108) style:UITableViewStylePlain];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.rowHeight = 40;
    _tabView.backgroundColor = BgColor;
    _tabView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:_tabView];
    



}


-(void)setUIStoreRoomTwo
{
    UIView *aVi = [[UIView alloc] initWithFrame:CGRectMake(0, 64, B_WIDTH, 40)];
    aVi.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:aVi];
    NSArray *titArr = @[@"楼号",@"房号",@"优惠政策"];
    UILabel * tempView = [[UILabel alloc]init];
    // NSInteger count = 5;//设置一排view的个数
    NSInteger margin = 10;//设置相隔距离
    NSInteger height = 20;//设置view的高度
    for (int i = 0; i < titArr.count; i ++) {
        //UIView * view = [[UIView alloc]init];
        //view.backgroundColor = [UIColor brownColor];
        UILabel *lab = [[UILabel alloc] init];
        lab.textColor = [UIColor orangeColor];
        lab.font = [UIFont systemFontOfSize:14];
        lab.textAlignment = NSTextAlignmentCenter;
        lab.text = titArr[i];
        [aVi addSubview:lab];
        
        
        //[aVi addSubview:view];
        if (i == 0) {
            [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(aVi).offset(margin);
                make.centerY.equalTo(aVi);
                make.height.mas_equalTo(height);
            }];
        }
        else if (i == titArr.count -1){
            [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(aVi).offset(-margin);
                make.left.equalTo(tempView.mas_right).offset(margin);
                make.centerY.equalTo(tempView);
                make.height.equalTo(tempView);
                make.width.equalTo(tempView);
            }];
        }
        else{
            [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(tempView.mas_right).offset(margin);
                make.centerY.equalTo(tempView);
                make.height.equalTo(tempView);
                make.width.equalTo(tempView);
            }];
        }
        tempView = lab;
        [lab layoutIfNeeded];
    }
    
    
    
    
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _roomArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static  NSString *ID = @"Cell";
    StoreRoomTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell)
    {
        cell = [[StoreRoomTwoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    if ([cell isKindOfClass:[StoreRoomTwoCell class]])
    {
                if (_roomArr.count !=0)
                {
                    NSDictionary *dict = [_roomArr objectAtIndex:indexPath.row];
                    [(StoreRoomTwoCell *)cell setUpCeoStoreRoomTwoCellLou:dict[@"block_num"] withFangLab:dict[@"under_id"] withCeLab:dict[@"name"]];
                }
        //
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   ZhuShouThreeVC *zhuThreeVC = [[ZhuShouThreeVC alloc] init];
   zhuThreeVC.allID = [[_roomArr objectAtIndex:indexPath.row] objectForKey:@"under_id"];
   [self.navigationController pushViewController:zhuThreeVC animated:NO];
    
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 60)];
    footView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:footView];
    
    UILabel *linLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 0.5)];
    linLab.backgroundColor = [UIColor lightGrayColor];
    [footView addSubview:linLab];
    UIButton *agreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //agreeBtn.frame = CGRectMake(20, 5, 80, 30);
    agreeBtn.layer.cornerRadius = 5;
    agreeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [agreeBtn setTitle:@"同意执行" forState:0];
    [agreeBtn setTitleColor:[UIColor whiteColor] forState:0];
    [agreeBtn setBackgroundColor:[UIColor orangeColor]];
    [agreeBtn addTarget:self action:@selector(storeRoomAgreeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:agreeBtn];
    [agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 30));
        make.right.equalTo(footView.mas_centerX).offset(-10);
        make.top.equalTo(footView.mas_top).offset(5);
    }];
    
    UIButton *unAgreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    unAgreeBtn.layer.cornerRadius = 5;
    unAgreeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [unAgreeBtn setTitle:@"驳回修改" forState:0];
    [unAgreeBtn setTitleColor:[UIColor whiteColor] forState:0];
    [unAgreeBtn setBackgroundColor:[UIColor orangeColor]];
    [unAgreeBtn addTarget:self action:@selector(storeRoomUnAgreeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:unAgreeBtn];
    [unAgreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 30));
        make.left.equalTo(footView.mas_centerX).offset(10);
        make.top.equalTo(footView.mas_top).offset(5);
    }];
    return footView;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if ([_chuPiStr isEqualToString:@"1"])
    {
        return 40;
    }
    else
    {
        return 0;
    }
}
#pragma mark-----底部的按钮点击事件

-(void)storeRoomAgreeBtn:(UIButton *)btn
{
      NSString *IDA = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
   
  [_manager setUpCeoShenPiYouHuiAgreeWithType:@"4" withProjectID:_chuID withUID:IDA];
    
}
-(void)storeRoomUnAgreeBtn:(UIButton *)btn
{
   NSString *IDA = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
 [_manager setUPCeoYouHUIPiUnAgreeWithType:@"4" WithID:_chuID withUid:IDA];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
